﻿//==============================================================================
// File: LanguageService.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using WpfRcp.Controls.Utils;
using WpfRcp.Core.Common;
using WpfRcp.Core.Common.Localization;
using WpfRcp.Core.Globals;
using WpfRcp.Core.Services;
using WpfRcp.Helpers;
using Microsoft.Practices.Composite.Events;

namespace WpfRcp.Services
{
    public class LanguageService : BindableBase, ILanguageService
    {
        private readonly IEventAggregator m_EventAggregator;
        private readonly IApplicationSettings m_Settings;

        private static readonly IList<CultureInfo> SupportedLanguagesList = new List<CultureInfo>
        {
            new CultureInfo("en-US"),
            new CultureInfo("pl-PL")
        };

        public LanguageService(IEventAggregator eventAggregator, IApplicationSettings settings)
        {
            m_EventAggregator = eventAggregator;
            m_Settings = settings;
            m_Settings.DefaultsRestored += delegate { CurrentCulture = m_Settings.CurrentCulture; };
        }

        public IList<CultureInfo> SupportedLanguages
        {
            get { return SupportedLanguagesList; }
        }

        public CultureInfo CurrentCulture
        {
            get { return m_Settings.CurrentCulture; }
            set
            {
                if (value != null)
                {
                    m_Settings.CurrentCulture = value;
                    SetLanguage(value);

                    OnPropertyChanged("CurrentCulture");
                }
            }
        }

        public void SetLanguage(CultureInfo culture)
        {
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

            // Rebind the UI
            ResHelper.Instance.RebindAll();

            // Set the language inside common controls
            LanguageManager.SetLanguage(culture);

            CultureManager.UICulture = culture;

            // Publish event
            OnLanguageChanged();
        }

        private void OnLanguageChanged()
        {
            m_EventAggregator.GetEvent<LanguageChangedEvent>().Publish(CurrentCulture);
        }
    }
}